﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace TS_XY_Train_Repaire.Materials
{
    public partial class Materials_Manage_Details : DevExpress.XtraEditors.XtraForm
    {
        string UrlName=""; /* 图片处理*/
        string str="";
        byte[] imgBytesIn;
        string base64String="";
        public LeverString_SysTreeMainList _tl = new LeverString_SysTreeMainList();//分类值
        public string _Select;
        public string _LoadType;
        public string _code;
        public string _name;
        public string _code_map;
        public string _code_factory;
        public string _code_station;
        public string _code_type;
        public string _cz;
        public string _unit;
        public string _num;
        public string _upnum;
        public string _lownum;
        public string _indate;
        public string _worktype;
        public string _changetype;
        public string _id;
        public int _del; 


       
 

        public Materials_Manage_Details()
        {
            InitializeComponent();
        }

        private void rb_class_CheckedChanged(object sender, EventArgs e)
        {
            groupClass.Visible = true;
            groupDetailed.Visible = false;
        }

        private void rb_camera_CheckedChanged(object sender, EventArgs e)
        {
            groupClass.Visible = false;
            groupDetailed.Visible = true;
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {

            this.DialogResult = DialogResult.Cancel;
        }

        private void Materials_Manage_Details_Load(object sender, EventArgs e)
        {
            if (_Select == "1")
            {
                rb_class.Checked = false;
                rb_Materials.Checked = true;
                if (_LoadType == "Add")
                {
                    lbClass.Text = "分类名称:" + _tl.f_name;
                    bt_Sure.Text = "新  增";
                }

                else if (_LoadType == "Edit")
                {
                    SelectPicture(); 
                    rb_class.Visible = false;
                    readAll();
                    bt_Sure.Text = "修  改";
                    cb_lx.Visible = false;
                    tb_code.Enabled = false;
                }
                else
                {
                    SelectPicture(); 
                    rb_class.Visible = false;
                    readAll();
                    groupDetailed.Enabled = false;
                    bt_Sure.Text = "删  除";
                    
                    cb_lx.Visible = false;
               
                }
            }
            else
            {
                rb_class.Checked = true;
                rb_Materials.Checked = false;
                lbClass.Text = "本级分类名称：" + _tl.f_name;

                if (_LoadType == "Add")
                {
                    if (_tl.f_id == "M-1")
                    {
                        rb_Materials.Visible = false;
                        lbClass.Text = "上级分类名称：" + _tl.f_name;
                        bt_Sure.Text = "新  增";
                        tb_classCode.MaxLength = 1;   //分类代码仅仅只能输入一位。
                    }
                    else
                    {
                        lbClass.Text = "上级分类名称：" + _tl.f_name;
                        bt_Sure.Text = "新  增";
                        groupClass.Enabled = false;    //不可以添加分类
                        rb_class.Enabled = false;      //不可以添加分类
                        rb_Materials.Checked = true;   //显示明细添加
                     }
                }
                else if (_LoadType == "Edit")
                {
                    rb_Materials.Visible = false;
                    tb_classCode.Text = _tl.f_code;
                    tb_class.Text = _tl.f_name;
                    bt_Sure.Text = "修  改";
                    cb_lx.Visible = false;
                    tb_classCode.Enabled = false;

                }
                else
                {
                    rb_Materials.Visible = false;
                    tb_classCode.Text = _tl.f_code;
                    tb_class.Text = _tl.f_name;
                    tb_classCode.Enabled = false;
                    tb_class.Enabled = false;
                    bt_Sure.Text = "删  除";
                    cb_lx.Visible = false;
                    MessageBox.Show("一级分类不允许删除！");
                    bt_Sure.Enabled = false;
                }
            }
        }

        private void readAll()
        {
            tb_code.Text = _code;
            tb_code_factory.Text = _code_factory;
            tb_code_map.Text = _code_map;
            tb_code_station.Text = _code_station;
            tb_code_type.Text = _code_type;
            tb_name.Text = _name;
            tb_num.Text = _num;
            tb_unit.Text = _unit;
            tb_upnum.Text = _upnum;
            tb_lownum.Text = _lownum;
            tb_indate.Text = _indate;
            cb_worktype.SelectedIndex = Convert.ToInt32(_worktype);
            cb_changetype.SelectedIndex = Convert.ToInt32(_changetype);
            tb_cz.Text = _cz;
        }

        private void clearAll()
        {
            tb_code.Text = "";
            tb_code_factory.Text = "";
            tb_code_map.Text = "";
            tb_code_station.Text = "";
            tb_code_type.Text = "";
            tb_name.Text = "";
            tb_num.Text = "";
            tb_unit.Text = "";
            tb_upnum.Text = "";
            tb_lownum.Text = "";
            tb_indate.Text = "";
            tb_cz.Text = "";
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {   //分类
            if (rb_class.Checked)
            {
                string strSQL;
                if (_LoadType == "Add")
                {
                    if (_tl.f_id == "M-1")
                    {
                        string _Ams = "Select count(*) as f_code from TS_S_T_B_Materials where f_type=0 and f_del=0 and f_code='" + tb_classCode.Text.Trim() + "'";
                        string _Acode = TS_Win_Class.GetSQlFieldValue_1(_Ams, "f_code");
                        if (_Acode =="0" )
                        {
                            strSQL = "insert into TS_S_T_B_Materials ( f_code,f_name,f_level,f_deep,f_type,f_workerid) Values('" + tb_classCode.Text.Trim() + "','" + tb_class.Text.Trim() + "','" + _tl.f_level + "'," + _tl.f_deep + ",'0','" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "') ";
                            TS_Win_Class.MySqlCom(strSQL);
                            if (cb_lx.Checked)
                            {
                                MessageBox.Show("添加数据成功！");
                                tb_class.Text = "";
                            }
                            else
                            {
                                MessageBox.Show("添加数据成功！");
                                this.DialogResult = DialogResult.OK;
                            }

                        }
                        else
                        {
                            MessageBox.Show("分类编码不能重复");
                            tb_classCode.Focus();

                        }
                    }
                    else
                    {
                            string _level = "";
                            int _deep = -1;
                            _level = _tl.f_level + _tl.f_id + ",";
                            _deep = Convert.ToInt32(_tl.f_deep) + 1;
                            strSQL = "insert into TS_S_T_B_Materials ( f_code,f_name,f_level,f_deep,f_type,f_workerid) Values('" + tb_classCode.Text.Trim() + "','" + tb_class.Text.Trim() + "','" + _level + "'," + _deep + ",'0','" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "') ";
                            TS_Win_Class.MySqlCom(strSQL);
                            if (cb_lx.Checked)
                            {
                                MessageBox.Show("添加数据成功！");
                                tb_class.Text = "";
                            }
                            else
                            {
                                MessageBox.Show("添加数据成功！");
                                this.DialogResult = DialogResult.OK;
                            }

                    }

                }

                else
                    if (_LoadType == "Edit")
                {

                    strSQL = "update TS_S_T_B_Materials set [f_name] = '" + tb_class.Text.Trim() + " ' ,[f_code] = '" + tb_classCode.Text.Trim() + " ' where f_id = " + _tl.f_id + "";
                    TS_Win_Class.MySqlCom(strSQL);
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    strSQL = "update TS_S_T_B_Materials set f_del= 1 ,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id = " + _tl.f_id + " or f_level like '%," + _tl.f_id + ",%' and f_del=0";
                    TS_Win_Class.MySqlCom(strSQL);
                    MessageBox.Show("删除数据成功！");
                    this.DialogResult = DialogResult.OK;
                }

            }
            else
            {//物资
                string StrSql;
                if (_LoadType == "Add")
                {
                    //if (tb_name.Text.Trim() != "" && tb_code.Text.Trim().Length >11 && tb_code_factory.Text.Trim() != "" && tb_code_map.Text.Trim() != "" && tb_code_type.Text.Trim() != "" && tb_num.Text.Trim() != "" && tb_unit.Text.Trim() != "" && tb_upnum.Text.Trim() != "" && tb_lownum.Text.Trim() != "" && tb_indate.Text.Trim() != "" && tb_cz.Text.Trim() != "" && tb_code_station.Text.Trim() != "")
                    if (tb_name.Text.Trim() != "" && tb_code_type.Text.Trim() != "" && tb_unit.Text.Trim() != "" && tb_code.Text.Trim() != "" && cb_worktype.Text.Trim() != "" && cb_changetype.Text.Trim() != "")
                    {
                        string _Bms = "Select count(*) as f_code from TS_S_T_B_Materials where f_type=1 and f_del=0 and f_code='" + tb_code.Text.Trim() + "'";
                        string _Bcode = TS_Win_Class.GetSQlFieldValue_1(_Bms, "f_code");
                        if (_Bcode == "0")
                        {

                            if (UrlName != "")
                            {
                                string _level = "";
                                int _deep = 0;
                                _level = _tl.f_level + _tl.f_id + ",";
                                _deep = Convert.ToInt32(_tl.f_deep) + 1;
                                StrSql = "INSERT INTO TS_S_T_B_Materials (f_name, f_code, f_code_map, f_code_factory, f_code_station, f_code_type, f_cz, f_unit, f_level, f_deep, f_num, f_upnum, f_lownum,  f_indate,f_type, f_worktype, f_changetype,f_workerid)";
                                string sql = " VALUES('" + tb_name.Text.Trim() + "','" + tb_code.Text.Trim() + "','" + tb_code_map.Text.Trim() + "','" + tb_code_factory.Text.Trim() + "','" + tb_code_station.Text.Trim() + "','" + tb_code_type.Text.Trim() + "','" + tb_cz.Text.Trim() + "','" + tb_unit.Text.Trim() + "','" + _level + "','" + _deep + "','" + tb_num.Text.Trim() + "','" + tb_upnum.Text.Trim() + "','" + tb_lownum.Text.Trim() + "','" + tb_indate.Text.Trim() + "','1','" + cb_worktype.SelectedIndex + "','" + cb_changetype.SelectedIndex + "','" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "') ";
                                string _MID = TS_Win_Class.MySqlComGetID(StrSql + sql);
                                FileStream fs = new FileStream(UrlName, FileMode.Open, FileAccess.Read);
                                System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                                imgBytesIn = br.ReadBytes(Convert.ToInt32(fs.Length));
                                base64String = Convert.ToBase64String(imgBytesIn);
                                string Resourcesql = "insert into TS_S_T_B_Materials_Resource([f_mid],[f_filename],[f_name],[f_brief],[f_workerid])values('" + _MID + "','" + UrlName + "','" + str + "','" + base64String + "','" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "')";
                                TS_Win_Class.MySqlCom(Resourcesql);
                            }
                            else
                            {
                                string _level = "";
                                int _deep = 0;
                                _level = _tl.f_level + _tl.f_id + ",";
                                _deep = Convert.ToInt32(_tl.f_deep) + 1;
                                StrSql = "INSERT INTO TS_S_T_B_Materials (f_name, f_code, f_code_map, f_code_factory, f_code_station, f_code_type, f_cz, f_unit, f_level, f_deep, f_num, f_upnum, f_lownum,  f_indate,f_type, f_worktype, f_changetype,f_workerid)";
                                string sql = " VALUES('" + tb_name.Text.Trim() + "','" + tb_code.Text.Trim() + "','" + tb_code_map.Text.Trim() + "','" + tb_code_factory.Text.Trim() + "','" + tb_code_station.Text.Trim() + "','" + tb_code_type.Text.Trim() + "','" + tb_cz.Text.Trim() + "','" + tb_unit.Text.Trim() + "','" + _level + "','" + _deep + "','" + tb_num.Text.Trim() + "','" + tb_upnum.Text.Trim() + "','" + tb_lownum.Text.Trim() + "','" + tb_indate.Text.Trim() + "','1','" + cb_worktype.SelectedIndex + "','" + cb_changetype.SelectedIndex + "','" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "') ";
                                string _MID = TS_Win_Class.MySqlComGetID(StrSql + sql);
                            }

                            if (cb_lx.Checked)
                            {
                                MessageBox.Show("添加数据成功！");
                                clearAll();
                            }
                            else
                            {
                                MessageBox.Show("添加数据成功！");
                                this.DialogResult = DialogResult.OK;
                            }

                        }
                        else 
                        {
                            MessageBox.Show("物资编码不能重复！");
                            tb_code.Focus();
                        }

                    }
                    else
                    {
                        MessageBox.Show("有文本框未输入值!或者物资编码不足12位！");
                    }
                }
                else
                    if (_LoadType == "Edit")
                    {
                        if (tb_name.Text.Trim() != "" && tb_code_type.Text.Trim() != "" && tb_unit.Text.Trim() != "" && tb_code.Text.Trim() != "" && cb_worktype.Text.Trim() != "" && cb_changetype.Text.Trim() != "")
                        {
                            if (UrlName != "")
                            {
                                StrSql = "update TS_S_T_B_Materials set f_name='" + tb_name.Text.Trim() + "', f_code='" + tb_code.Text.Trim() + "', f_code_map='" + tb_code_map.Text.Trim() + "', f_code_factory='" + tb_code_factory.Text.Trim() + "', f_code_station='" + tb_code_station.Text.Trim() + "', f_code_type='" + tb_code_type.Text.Trim() + "', f_cz='" + tb_cz.Text.Trim() + "', f_unit='" + tb_unit.Text.Trim() + "',f_num='" + tb_num.Text.Trim() + "', f_upnum='" + tb_upnum.Text.Trim() + "', f_lownum='" + tb_lownum.Text.Trim() + "',  f_indate='" + tb_indate.Text.Trim() + "', f_worktype='" + cb_worktype.SelectedIndex + "', f_changetype='" + cb_changetype.SelectedIndex + "',f_workerid='" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "' where f_id=" + _id;
                                TS_Win_Class.MySqlCom(StrSql);
                                //数据出入

                                FileStream fs = new FileStream(UrlName, FileMode.Open, FileAccess.Read);
                                System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                                imgBytesIn = br.ReadBytes(Convert.ToInt32(fs.Length));
                                base64String = Convert.ToBase64String(imgBytesIn);
                                string _str = TS_Win_Class.GetSQlFieldValue("select count(*) as f_count from TS_S_T_B_Materials_Resource where f_mid=" + _id + "", "f_count");
                                if (_str != "0")
                                {
                                    string pictureSql = "update TS_S_T_B_Materials_Resource set f_filename='" + UrlName + "',f_name='" + str + "',f_brief='" + base64String + "' where f_mid='" + _id + "'";
                                    TS_Win_Class.MySqlCom(pictureSql);
                                }
                                else
                                {
                                    string pictureSql = "insert into TS_S_T_B_Materials_Resource([f_mid],[f_filename],[f_name],[f_brief],[f_workerid])values('" + _id + "','" + UrlName + "','" + str + "','" + base64String + "','" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "')";
                                    TS_Win_Class.MySqlCom(pictureSql);
                                }
                            }
                            else 
                            {
                                StrSql = "update TS_S_T_B_Materials set f_name='" + tb_name.Text.Trim() + "', f_code='" + tb_code.Text.Trim() + "', f_code_map='" + tb_code_map.Text.Trim() + "', f_code_factory='" + tb_code_factory.Text.Trim() + "', f_code_station='" + tb_code_station.Text.Trim() + "', f_code_type='" + tb_code_type.Text.Trim() + "', f_cz='" + tb_cz.Text.Trim() + "', f_unit='" + tb_unit.Text.Trim() + "',f_num='" + tb_num.Text.Trim() + "', f_upnum='" + tb_upnum.Text.Trim() + "', f_lownum='" + tb_lownum.Text.Trim() + "',  f_indate='" + tb_indate.Text.Trim() + "', f_worktype='" + cb_worktype.SelectedIndex + "', f_changetype='" + cb_changetype.SelectedIndex + "',f_workerid='" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "' where f_id=" + _id;
                                TS_Win_Class.MySqlCom(StrSql);
                            }
                           
                            MessageBox.Show("修改数据成功！","系统提示 ！");
                            clearAll();
                            this.DialogResult = DialogResult.OK;
                        }
                        else
                        {
                            MessageBox.Show("有文本框未输入值！");
                        }
                    }
                    else
                    {
                        SelectPicture();


                            StrSql = "update TS_S_T_B_Materials set f_del=1 where f_id= " + _id;
                            TS_Win_Class.MySqlCom(StrSql);

                            string PictureDel = "update TS_S_T_B_Materials_Resource set f_del=1 where f_mid= '" + _id + "'";
                            TS_Win_Class.MySqlCom(PictureDel);
                            MessageBox.Show("删除数据成功！","系统提示！");
                            clearAll();
                            this.DialogResult = DialogResult.OK;
 
                    }
            }
        }
       

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPG文件|*.jpg|PNG图片|*.png";
            if (DialogResult.OK == openFileDialog1.ShowDialog())
            { if (openFileDialog1.FileName != "")
                {  
                    pictureBox1.ImageLocation = openFileDialog1.FileName.ToString();
                      UrlName = pictureBox1.ImageLocation; //获取图片路径
                      string[] sArray = UrlName.Split('\\');               
                      str = sArray[sArray.Length - 1];//获取图片的名称                    
                    // tb_code_map.Text = str;
                    //执行数据插入
                }
          }
        }
       
        public void SelectPicture() {
        
 
            MemoryStream mss = new MemoryStream();
            string pictureSql = "select * from dbo.TS_S_T_B_Materials_Resource where f_mid='"+_id+"'";
            SqlConnection con = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            con.Open();
            SqlCommand com = new SqlCommand(pictureSql,con);
            SqlDataReader der = com.ExecuteReader();
            while(der.Read()){
                MemoryStream ms = new MemoryStream(Convert.FromBase64String(der["f_brief"].ToString()));
                Image image = Image.FromStream(ms, true);
                pictureBox1.Image = image;
                str=der["f_name"].ToString();
                UrlName = der["f_filename"].ToString();
             }
          
            //this.pictureBox1.Image = Image.FromStream(mss); 
            con.Close();
            der.Close();
            der.Dispose();      
        }


        private void Materials_Manage_Details_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
          //MessageBox.Show("点击选择照片!");
        }

        private void tb_num_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            } 
        }

        private void tb_indate_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            } 
        }

        private void tb_upnum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            } 
        }

        private void tb_lownum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            } 
        }

        private void tb_code_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            } 

        }

    }
}


